<?php
namespace Home\Event;

class SeckillEvent{
    
    
    /**
     * 统计秒杀商品费用
     * @param int $itemId
     */
    public function getCount(){
        //获取购物车或者直从cookie中获取接购买的商品信息
        $item = array(unserialize(cookie('__buynow__')));
        $data = $item[0];
        $supp_id = $data['supplier_id'] ? : 0;
        $store_id=$data['store_id'] ? : 0;
        //商品种类数
        $result['total_num'] =1;
        //数量
        $result['total_quantity'] = $data['quantity'];
        //重量
        $result['total_weight'] = $data['weight'] * $data['quantity'];
        //根据商品id和规格获取商品价格
        $price = $data['price'];
       // dump($price);die;
     //   $price = D('Order')->getItemPrice($data['item_id'], $data['item_code'],$isKill=true);
      //  dump($price);die;
        $result['total_price'] = $price * $data['quantity'];
        //小计
        $result['subAmount'] = $result['total_price'];
        //供应商
        $result['supplier_name'] = get_supplier_text($supp_id);
        $result['store_name'] = get_store_text($store_id);

        //最低免运费额度

        $free_amount = get_store_free_amount($store_id);
        $free_amount = (int)$free_amount;
        if(($free_amount == 0 || $result['total_price'] < $free_amount) ) {
            $tpl_list = M('TransportExtend')->where(array('status' => 1, 'transport_id' => $data['transport_id']))->order('sort asc')->select();
            if(empty($tpl_list)){
                $result['delivery'] = $data['freight'];
            }else{
                foreach ($tpl_list as $line) {
                    $arr = array(
                        'id' => $line['id'],
                        'name' => $line['area_name'],
                        'price_type' => $line['price_type'],
                        'price' => $line['express_postage'],
                    );
                    if ($line['price_type'] == 1) { //按件数计费
                        if ($result['total_quantity'] <= $line['express_start']) {
                            $arr['price'] = $line['express_postage'];
                        } else {
                            $plus_w = $result['total_quantity'] - $line['express_start'];
                            $arr['price'] = number_format($plus_w * $line['express_postageplus'] + $line['express_postage'], 2);
                        }
                    } else if ($line['price_type'] == 2) { //按重量计费
                        if ($result['total_weight'] <= $line['express_start']) {
                            $arr['price'] = $line['express_postage'];
                        } else {
                            $plus_w = $result['total_weight'] - $line['express_start'];
                            $arr['price'] = ceil($plus_w) * $line['express_postageplus'] + $line['express_postage'];
                        }
                    }
                    $arr['price'] = sprintf('%.2f', $arr['price']);
                    //   $store['delivery'][$arr['id']] = $arr;
                    $result['delivery'][$arr['id']] = $arr;
                }
            }

        }else{
            $result['delivery'] = 0;
        }
        $result = array_merge($data,$result);
        return $result;

    }
    
    
    /**
     * 获取秒杀信息
     * @param int $itemId    商品id
     * @param int $code      秒杀规格
     * @return \Think\mixed
     */
    public function getInfo($itemId){
        $map['status'] = 1;
        $map['item_id'] = intval($itemId);
        $map['stime'] = array('lt',time());
        $map['etime'] = array('gt',time());
       // $map['item_spc'] = $code;
        $data = M('seckill_item')->field('id',true)->where($map)->find();
        //判断秒杀活动是否呗商户删除
        $map['_string'] = 'FIND_IN_SET('.$map['item_id'].',item_ids)';
        $count = M('seckill')->where($map)->count();
        if ($count > 0) {
            return  $data;
        }
        
    }
    
    
    /**
     * 获取商品详细信息
     */
    public function itemDetail($info){
        //获取封面图片路径
        if($info['thumb']){
            $info['cover_path'] = get_cover($info['thumb'], 'path');
        }
        
        //获取产品图片路径数组
        if($info['images']){
            $info['images_list'] = get_images_info($info['images'], 'id, path');
        }
        
        //获取此商品参加秒杀的规格
        $seckill_idKeys = array();
        $seckill = A('Item','Event')->seckillData($item_id);
        if(!empty($seckill)){
            $seckill_idKeys = array_column($seckill, null , 'item_id');
        }
        
        //获取商品属性相关的所有数据（属性项，属性值，规格-库存-价格配置数据）
        $info['property'] = $this->getPropertyAll($item_id,$seckill_idKeys);
        
        //查询最高价
        $max_price = D('ItemSpecifiction')->getMaxPrice($item_id);
        
        //查询最低价
        $min_price = D('ItemSpecifiction')->getMinPrice($item_id);
        
        //设置价格区间
        if($max_price && $min_price && $max_price!==$min_price){
            $info['price'] = $min_price . '~' . $max_price;
        }
        
        //获取运费
        $delivery_id = $info['delivery_id'];
        if($delivery_id){
            $info['delivery'] = M('DeliveryTpl')->where('id = '.$delivery_id)->find();
        }
        
        //商品介绍HMTL处理
        $info['intro'] = html_entity_decode($info['intro']);
        
        return $info;
    }

    /**
     * 获取参加秒杀活动的商品的id
     * @return multitype:
     */
    public function getItemIds(){
        //查询已参加秒杀活动的商品
        $map['stime'] = array('elt',NOW_TIME);
        $map['etime'] = array('gt',NOW_TIME);
        $map['status'] = array('eq',1);
        $list = M('seckill')->field('item_ids')->where($map)->select();
        $ids = array();
        if(!empty($list)){
            foreach($list as $key=>$val){
                //$ids = getUniqure( explode(',' , $val['item_ids']));
                $ids = array_merge($ids,getUniqure( explode(',' , $val['item_ids'])));
            }
        }

        //已参加复购活动的商品
        $map1['status'] = array('eq',1);
        $list = M('Fugou')->field('item_id')->where($map)->select();
        $ids1 = array();
        if(!empty($list)){
            foreach($list as $key=>$val){
                $ids1[$key] = $val['item_id'];
            }
        }

        //参加买送活动的商品
        $map2['status'] = array('eq',1);
        $list = M('BuySend')->field('item_id')->where($map)->select();
        $ids2 = array();
        if(!empty($list)){
            foreach($list as $key=>$val){
                $ids2[$key] = $val['item_id'];
            }
        }

        //参加第二件折扣的商品
        $map3['status'] = array('eq',1);
        $list = M('SecondPieces')->field('item_id')->where($map)->select();
        $ids3 = array();
        if(!empty($list)){
            foreach($list as $key=>$val){
                $ids3[$key] = $val['item_id'];
            }
        }

        $ids4 = array();
        $ids4 = getUniqure(array_merge($ids,$ids1,$ids2,$ids3));

        return $ids4;
    }

}